package com.jd.tuning.jvm.threadpool;

import java.util.Date;
import java.util.concurrent.*;

/**
 * 线程池2
 */
public class ScheduledThreadPoolExecutorTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledThreadPoolExecutor threadPoolExecutor=
                new ScheduledThreadPoolExecutor(10,
                        Executors.defaultThreadFactory(),
                        new ThreadPoolExecutor.AbortPolicy());

//        //延时3秒之后再去执行任务
//        threadPoolExecutor.schedule(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("aaa");
//            }
//        },3,TimeUnit.SECONDS);
//
//        //延时3秒之后再去执行任务
//       ScheduledFuture<String> future= threadPoolExecutor.schedule(new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                return "bb";
//            }
//        },4,TimeUnit.SECONDS);
//        System.out.println(future.get());


        threadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduleAtFixedRate:"+new Date());
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },0,        //第一次执行任务时，延时多久
                3,     //每个多久执行这个任务
                TimeUnit.SECONDS);

        threadPoolExecutor.scheduleWithFixedDelay(new Runnable() {
                                                   @Override
                                                   public void run() {
                                                       System.out.println("scheduleWithFixedDelay:"+new Date());
                                                       try {
                                                           Thread.sleep(1000L);
                                                       } catch (InterruptedException e) {
                                                           e.printStackTrace();
                                                       }
                                                   }
                                               },0,        //第一次执行任务时，延时多久
                3,     //每次执行完任务之后，延迟多久再次执行这个任务
                TimeUnit.SECONDS);

    }


}
